import { Controller, Get, Post, Delete,Body, Param, UseGuards, Request, Query, UseInterceptors, UploadedFiles, NotFoundException, BadRequestException, ParseIntPipe, Put } from '@nestjs/common';
import { ExamsService } from './exams.service';
import { Roles } from '../auth/roles.decorator'; // 引入 @Roles 装饰器
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard'; // 导入角色守卫



@Controller('exams')
@UseGuards(JwtAuthGuard, RolesGuard) // 应用 JwtAuthGuard 和 RolesGuard
export class ExamsController {
  constructor(private readonly examsService: ExamsService) {}
  // 获取所有考卷
  @Get()// 使用 @Roles 装饰器
  async findAll() {

    return this.examsService.findAll();
  }

 // 创建考卷（支持分块配置）
 @Post('/create')
 @Roles('ADMIN')
 async create(@Body() createExamDto: { 
   title: string;
   type:'1'|'2'|'3';
   detailType:'1'|'2'|'3';
   sections: Array<{
     name: string;
     duration: number;
     subType: string;
     minDuration?: number;
     questions: string[]; // Question ID 数组
   }>;
 }) {
   return this.examsService.createExam(createExamDto);
 }
 
 // 删除考卷
 @Delete('/:id')
 @Roles('ADMIN')
 async remove(@Param('id') id: string) {
  console.log('id',id)
    this.examsService.remove(id);
    return { code: 200, message: '删除成功' };
 }


  // 通过type和detailType ，subType 获取考卷
  @Get('/bytType')
  async findByType(@Query('type') type: string, @Query('detailType') detailType: string, @Query('subType') subType: string) {
    return this.examsService.findByType(type, detailType, subType); 
  }


 // 获取考卷详情（填充分块中的题目）
 @Get('/getExamDetail')
 async getExamDetail(@Query('examId') examId: string) {
   return this.examsService.getExamDetail(examId);
 }
// 添加单独题目到考卷分块
@Post(':examId/sections/:sectionIndex/questions')
@Roles('ADMIN')
async addQuestionToSection(
  @Param('examId') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number,
  @Body('questionId') questionId: string
) {
  try {
    const updatedExam = await this.examsService.addQuestionToSection(
      examId,
      sectionIndex,
      questionId
    );
    
    return {
      code: 200,
      message: '题目添加成功',
      data: updatedExam
    };
  } catch (error) {
    if (error instanceof NotFoundException) {
      return {
        code: 404,
        message: error.message
      };
    }
    if (error instanceof BadRequestException) {
      return {
        code: 400,
        message: error.message
      };
    }
    return {
      code: 500,
      message: '服务器内部错误'
    };
  }
}

// 添加题目组到考卷分块
@Post(':examId/sections/:sectionIndex/question-groups')
@Roles('ADMIN')
async addQuestionGroupToSection(
  @Param('examId') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number,
  @Body('groupId') groupId: string
) {
  try {
    const updatedExam = await this.examsService.addQuestionGroupToSection(
      examId,
      sectionIndex,
      groupId
    );
    
    return {
      code: 200,
      message: '大题组添加成功',
      data: updatedExam
    };
  } catch (error) {
    if (error instanceof NotFoundException) {
      return {
        code: 404,
        message: error.message
      };
    }
    if (error instanceof BadRequestException) {
      return {
        code: 400,
        message: error.message
      };
    }
    return {
      code: 500,
      message: '服务器内部错误'
    };
  }
}

// 从考卷分块中移除单独题目
@Delete(':examId/sections/:sectionIndex/questions/:questionId')
@Roles('ADMIN')
async removeQuestionFromSection(
  @Param('examId') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number,
  @Param('questionId') questionId: string
) {
  try {
    const updatedExam = await this.examsService.removeQuestionFromSection(
      examId,
      sectionIndex,
      questionId
    );
    
    return {
      code: 200,
      message: '题目移除成功',
      data: updatedExam
    };
  } catch (error) {
    if (error instanceof NotFoundException) {
      return {
        code: 404,
        message: error.message
      };
    }
    if (error instanceof BadRequestException) {
      return {
        code: 400,
        message: error.message
      };
    }
    return {
      code: 500,
      message: '服务器内部错误'
    };
  }
}

// 从考卷分块中移除题目组
@Delete(':examId/sections/:sectionIndex/question-groups/:groupId')
@Roles('ADMIN')
async removeQuestionGroupFromSection(
  @Param('examId') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number,
  @Param('groupId') groupId: string
) {
  try {
    const updatedExam = await this.examsService.removeQuestionGroupFromSection(
      examId,
      sectionIndex,
      groupId
    );
    
    return {
      code: 200,
      message: '大题组移除成功',
      data: updatedExam
    };
  } catch (error) {
    if (error instanceof NotFoundException) {
      return {
        code: 404,
        message: error.message
      };
    }
    if (error instanceof BadRequestException) {
      return {
        code: 400,
        message: error.message
      };
    }
    return {
      code: 500,
      message: '服务器内部错误'
    };
  }
}

// 获取考卷详情（填充分块中的题目）
@Get(':id')
async figetQuestionDetaildOne(@Param('id') id: string) {
  return this.examsService.getQuestionDetail(id);
}
// 添加新的分块
@Post(':id/sections')
@Roles('ADMIN')
async addSection(
  @Param('id') examId: string,
  @Body() sectionData: { name: string; subType: string; duration: number; minDuration?: number }
) {
  return this.examsService.addSection(examId, sectionData);
}

// 删除分块
@Delete(':id/sections/:sectionIndex')
@Roles('ADMIN')
async removeSection(
  @Param('id') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number
) {
  return this.examsService.removeSection(examId, sectionIndex);
}

// 更新分块
@Put(':id/sections/:sectionIndex')
@Roles('ADMIN')
async updateSection(
  @Param('id') examId: string,
  @Param('sectionIndex', ParseIntPipe) sectionIndex: number,
  @Body() updateData: { name?: string; subType?: string; duration?: number; minDuration?: number }
) {
  return this.examsService.updateSection(examId, sectionIndex, updateData);
}
}